package com.sxds.wn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.sxds.wn.security.dto.User;

/**
 * Description: <p>系统登录控制器实现类</p>
 * Content Desc:<p>系统登录控制器实现类<p>
 * Copy Right of Personal Tangtao 2007-2020
 * @author Andy
 * @version 1.0 Create Date:@2008-11-12
 */
public class LogonController extends BaseFormController {
	
	/**
	 * 
	 * 默认构造方法
	 */
	public LogonController(){
		super();
	}
	
	/**
	 * 具体认证业务处理
	 */
	public ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors) throws Exception {
		User user = (User)command;
		HttpSession session = request.getSession(false);
		User sessionUser = (session!=null ? (User)session.getAttribute(USER_SESSION_KEY):null);
		if( sessionUser!=null && sessionUser.getUserName().equals(user.getUserName()) ){
			return new ModelAndView(getSuccessView());
		}else{
			session = request.getSession();
		}
		User persistenUser = securityService.authenticationUser(user);
		if( persistenUser==null ){
			//用户身份不存在系统中
			this.saveError(request, "用户名或密码错误");
			return showForm(request, response, errors);
			//return null;
		}
		//将用户信息写到会话对象中
		user.setUserId(persistenUser.getUserId());
		user.setUserName(persistenUser.getUserName());
		session.setAttribute(USER_SESSION_KEY, user);
		return new ModelAndView(getSuccessView());
	}

	@Override
	protected void processResponse() {
		
	}

	@Override
	protected void setRequestParamer() {
		
	}
}
